package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC838 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 感觉更多是奇技淫巧
     * 未来需要再次复习此道题目
     * @return:
     */

    class Solution {
        public String pushDominoes(String dominoes) {
            int N = dominoes.length();
            int[] indexes = new int[N + 2];
            char[] symbols = new char[N + 2];
            int len = 1;
            indexes[0] = -1;
            symbols[0] = 'L';

            for (int i = 0; i < N; ++i) {
                if (dominoes.charAt(i) != '.') {
                    indexes[len] = i;
                    symbols[len++] = dominoes.charAt(i);
                }
            }

            indexes[len] = N;
            symbols[len++] = 'R';

            char[] ans = dominoes.toCharArray();
            for (int index = 0; index < len - 1; ++index) {
                int i = indexes[index], j = indexes[index + 1];
                char x = symbols[index], y = symbols[index + 1];
                char write;
                if (x == y) {
                    for (int k = i + 1; k < j; ++k)
                        ans[k] = x;
                } else if (x > y) { // RL
                    for (int k = i + 1; k < j; ++k)
                        ans[k] = k - i == j - k ? '.' : k - i < j - k ? 'R' : 'L';
                }
            }

            return String.valueOf(ans);
        }
    }


    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.pushDominoes(".L.R...LR..L.."));
    }

    public static void main(String[] args) throws Exception {
        LC838 an = new LC838();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
